package com.ems.service.impl.supers.action;

import com.ems.dao.supers.action.GroupActionDao;
import com.ems.dao.supers.action.GroupDao;
import com.ems.pojo.permission.Action;
import com.ems.pojo.permission.Group;
import com.ems.pojo.permission.GroupAction;
import com.ems.service.supers.action.ActionService;
import com.ems.service.supers.action.GroupService;
import com.ems.service.supers.action.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 伟伟
 * @Date: 2019/10/5
 * @Description: 描述
 * @Version: 1.0
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupDao groupDao;

    @Autowired
    private ActionService actionService;

    @Autowired
    private GroupActionDao groupActionDao;

    @Autowired
    @Qualifier("UserGroupService")
    private UserGroupService userGroupService;

    /**
     * 获取所有角色信息
     * @return
     */
    @Override
    public List<Group> getAllGroup() {
        return groupDao.getAllGroup();
    }

    /**
     * 添加角色
     * @param group
     * @return
     */
    @Override
    public int addGroup(Group group) {
        return groupDao.addGroup(group);
    }

    /**
     * 修改角色
     * @param group
     * @return
     */
    @Override
    public int updateGroup(Group group) {
        return groupDao.updateGroup(group);
    }

    /**
     * 删除角色
     * @param group
     * @return
     */
    @Override
    public int deleteGroup(Group group) {

        //查询该角色下面的用户
        int n = userGroupService.getCountByGrId(group.getGr_id());
        if(n == 0){
            return groupDao.deleteGroup(group);
        }else{
            return -1;
        }
    }

    /**
     * 获取对应角色的权限树信息
     * @param gr_id
     * @return
     */
    @Override
    public List<Action> findActionTreeByGrId(int gr_id) {
        //获取所有权限树信息
        List<Action> actionTreeList = actionService.getAllAction();
        //获取group_action表中指定角色已拥有的权限信息
        List<GroupAction> groupActionList = groupActionDao.findAllActionByGroupId(gr_id);

        //组装权限树信息
        for(int i = 0;i < actionTreeList.size();i ++){
            if(actionTreeList.get(i).getList().size() > 0){
                for(int j = 0;j < actionTreeList.get(i).getList().size();j ++){
                    isHaveAction(actionTreeList.get(i).getList().get(j),groupActionList);
                }
            }else{
                isHaveAction(actionTreeList.get(i),groupActionList);
            }

        }

        return actionTreeList;
    }

    private void isHaveAction(Action action,List<GroupAction> groupActionList){
        for(int i = 0;i < groupActionList.size();i ++){
            if(action.getAction_id() == groupActionList.get(i).getAction_id()){
                action.setHave(true); //该角色拥有该权限
                action.setParent(false); //不是父权限，是子权限
                break;
            }
        }
    }
}
